package com.hafu.util;

import javax.lang.model.element.VariableElement;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/* 连接数据库功能*/

public class DbUtil {
    private static final String URL ="jdbc:mysql://localhost:3306/db_jxcms1?useUnicode=true&characterEncoding=utf-8&useSSL=false";
    //?useUnicode = true&characterEncoding=utf-8&useSSL=false这点可以不要
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";
    private static Connection connection = null;


    public static Connection getConn() {
        //获取Connection对象
        try {
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            System.out.println("数据库连接成功");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return connection;
    }
    public static void closeConn() {
        try{
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println(DbUtil.getConn());
    }
}
